package com.budwk.app.sys.controllers.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sys.models.LinuxPackageLay;
import com.budwk.app.sys.models.LinuxServiceLayConfig;
import com.budwk.app.sys.services.LinuxPackageLayService;
import com.budwk.app.sys.services.LinuxServicelayConfigService;
import com.budwk.app.sys.vo.LinuxPackageLayVo;
import com.budwk.app.sys.vo.LinuxServiceLayConfigVo;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;


@IocBean
@At("/platform/linux/package/lay")
@ApiDefinition(tag = "项目部署服务接口")
@Slf4j
public class LinuxPackageLayController {

    @Inject
    private LinuxPackageLayService linuxPackageLayService;
    @Inject
    private LinuxServicelayConfigService linuxServicelayConfigService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询列表查询")
    @ApiFormParams(implementation = LinuxPackageLayVo.class)
    @ApiResponses(implementation = LinuxPackageLay.class)
    @SaCheckLogin
    public Result<?> listByPage(@Param("..") LinuxPackageLayVo layVo) {
        log.info("LinuxpackagelayController listByPage param: {}", JSONObject.toJSONString(layVo));
        return linuxPackageLayService.listByPage(layVo);
    }

    @At
    @Ok("json")
    @DELETE
    @ApiOperation(name = "删除发布信息")
    @SaCheckLogin
    public Result<?> deletePackageLay(@Param("layId") String layId) {
        linuxPackageLayService.delete(layId);
        return Result.success();
    }


    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "发布")
    @SaCheckLogin
    public Result<?> pushPackage(@Param("layId") String layId, @Param("configId") String configId) throws InterruptedException {
        return linuxPackageLayService.pushPackage(layId, configId);
    }

    @AdaptBy(type = UploadAdaptor.class)
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "上传文件")
    @ApiFormParams(
            value = {@ApiFormParam(name = "file", description = "上传文件")},mediaType = "multipart/form-data"
    )
    @SaCheckLogin
    public Result<?> uploadFile(@Param("file") TempFile file, @Param("configId") String configId){
        return linuxPackageLayService.uploadFileToLinux(file, configId);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "重新上传文件")
    @SaCheckLogin
    public Result<?> afreshUploadFile(@Param("layId") String layId) {
        return linuxPackageLayService.afreshUploadFile(layId);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询列表查询")
    @ApiFormParams(implementation = LinuxServiceLayConfigVo.class)
    @ApiResponses(implementation = LinuxServiceLayConfig.class)
    @SaCheckLogin
    public Result<?> listConfigByPage(@Param("..") LinuxServiceLayConfigVo layVo) {
        log.info("LinuxpackagelayController listConfigByPage param: {}", JSONObject.toJSONString(layVo));
        return linuxServicelayConfigService.listConfigByPage(layVo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加配置信息")
    @ApiFormParams(implementation = LinuxServiceLayConfig.class)
    @SaCheckLogin
    public Result<?> addConfigInfo(@Param("..") LinuxServiceLayConfig layVo) {
        log.info("LinuxpackagelayController addConfigInfo param: {}", JSONObject.toJSONString(layVo));
        return linuxServicelayConfigService.addConfigInfo(layVo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改配置信息")
    @ApiFormParams(implementation = LinuxServiceLayConfig.class)
    @SaCheckLogin
    public Result<?> updateConfigInfo(@Param("..") LinuxServiceLayConfig layVo) {
        log.info("LinuxpackagelayController updateConfigInfo param: {}", JSONObject.toJSONString(layVo));
        return linuxServicelayConfigService.updateConfigInfo(layVo);
    }

    @At
    @Ok("json")
    @DELETE
    @ApiOperation(name = "删除配置信息")
    @SaCheckLogin
    public Result<?> deleteConfigInfo(@Param("configId") String configId) {
        linuxServicelayConfigService.delete(configId);
        return Result.success();
    }

    @At
    @GET
    @ApiOperation(name = "查看日志")
    public void queryLog(@Param("layId") String layId, HttpServletResponse response) throws IOException {
        linuxPackageLayService.queryLog(layId, response);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取历史日志列表")
    @SaCheckLogin
    public Result<?> listHistroyLog(@Param("configId") String configId, @Param("queryTime") LocalDate queryTime) {
        log.info("listHistroyLog start configId: {}", configId);
        return linuxPackageLayService.listHistroyLog(configId, queryTime);
    }

    @At
    @GET
    @ApiOperation(name = "下载历史日志")
    public void downloadHistroyLog(@Param("configId") String configId, @Param("logName") String logName, HttpServletResponse response) {
        linuxPackageLayService.downloadHistroyLog(configId, logName, response);
    }
}
